import tornado.web
import tornado.ioloop
import time
import os
import json
import time
import base64
import numpy as np
import uuid
from PIL import Image
from io import BytesIO
from config import *
from apphelper.image import union_rbox,adjust_box_to_origin,base64_to_PIL


billList = ['通用OCR','火车票','身份证']
"""通用OCR识别"""
def isTrue(txt):
    if isinstance(txt,str):
        txt=txt.upper()
        if txt == 'TRUE':
            return True
        else:
            return False
    else:
        return txt


class OCR(tornado.web.RequestHandler):
    def getImgNew(self):
        if len(self.request.files)==0:
            return
        file = self.request.files.get("file",[])
        if len(file) == 0:
            return
        img =  file[0].body
        imgString = base64.b64encode(img)
        img = base64_to_PIL(imgString)
        return img

    def getImgOld(self):
        imgString = self.get_body_argument("imgString",'').encode().split(b';base64,')[-1]
        img = base64_to_PIL(imgString)
        return img

    def get(self):
        post = {}
        post['postName'] = 'ocr'##请求地址
        post['height'] = 1000
        post['H'] = 1000
        post['width'] = 600
        post['W'] = 600
        post['billList'] = billList
        self.render('layui_ocr.html',post = post)

    def post(self):
        t = time.time()
        uidJob = uuid.uuid1().__str__()
        billModel = self.get_body_argument("billModel",'')
        textAngle = isTrue(self.get_body_argument("textAngle",False))
        textLine =  isTrue(self.get_body_argument("textLine",False))
        img =  self.getImgNew()
        if(img == None):
            return
        img = np.array(img)
        H,W = img.shape[:2]
        print(billModel)
        print(textAngle)
        print(textLine)

        res = {"res": [{"name": "0", "text": "\u7a7f\u8863\u708e\u70ed \u65e0\u9700\u5e26\u4f1e", "box": [75.66782480402239, 203.99246383313812, 563.7547412979313, 203.00635426029456, 563.8356576089672, 243.05686571982903, 75.74874111505821, 244.0429752926726]}, {"name": "1", "text": "\u4ea4\u901a\u4e00\u822c \u6d17\u8f66\u9002\u5408", "box": [75.20433265219012, 469.04042385909037, 556.2594490064371, 469.0174757075482, 556.2614988884586, 511.9885286281946, 75.2063825342116, 512.0114767797368]}, {"name": "2", "text": "\u7136", "box": [391.2038690935324, 568.116416117008, 589.6795902371889, 574.8787799589724, 584.2961309064676, 732.883583882992, 385.8204097628111, 726.1212200410276]}, {"name": "3", "text": "\u611f\u5192\u6613\u53d1 \u5ba4\u5185\u8fd0\u52a8", "box": [75.66767054230701, 741.9797055740128, 555.7088884906378, 741.0202451399725, 555.7898361122456, 781.5202944259872, 75.74861816391478, 782.4797548600275]}, {"name": "4", "text": "\u53e3", "box": [87.03809304360243, 832.750371691229, 229.9594801681501, 831.2487797579954, 231.46190695639757, 974.249628308771, 88.5405198318499, 975.7512202420046]}, {"name": "5", "text": "\u9632\u6652\u9ad8 \u667e\u6652\u9002\u5408", "box": [100.3739897839657, 1006.9611610386931, 565.7635192593497, 1006.0156927999349, 565.8468370127017, 1047.0273401931781, 100.45730753731775, 1047.9728084319365]}], "timeTake": 5.0081}
        self.write(json.dumps(res))
        # print(imgString)


if __name__ == "__main__":
    #POST()
    app = tornado.web.Application([(r'/ocr',OCR)],
                                  template_path='templates',
                                  static_path='static',
                                  debug=True)
    #绑定一个监听端口
    app.listen(8080)
    #启动web程序，开始监听端口的连接
    tornado.ioloop.IOLoop.current().start()
